home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Button / Sources / ButtonPart.cpp < prev    next >
Encoding:
Text File  |  1995-11-09  |  12.4 KB  |  428 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                ButtonPart.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //    Modified by M.Boetcher to accept Dropped and Pasted sounds
  8. //
  9. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef BUTTONPART_H
  14. #include "ButtonPart.h"
  15. #endif
  16.  
  17. #ifndef BUTTONDEF_H
  18. #include "ButtonDef.h"
  19. #endif
  20.  
  21. #ifndef BUTTONSEL_H
  22. #include "ButtonSel.h"
  23. #endif
  24.  
  25. #ifndef ACTIONS_H
  26. #include "Actions.h"
  27. #endif
  28.  
  29. // ----- Framework Layer -----
  30.  
  31. #ifndef FWPUSHBU_H
  32. #include "FWPushBu.h"
  33. #endif
  34.  
  35. // ----- OS Layer -----
  36.  
  37. #ifndef FWRECSHP_H
  38. #include "FWRecShp.h"
  39. #endif
  40.  
  41. #ifndef FWODGEOM_H
  42. #include "FWODGeom.h"
  43. #endif
  44.  
  45. #ifndef FWEVENT_H
  46. #include "FWEvent.h"
  47. #endif
  48.  
  49. #ifndef FWMEMMGR_H
  50. #include "FWMemMgr.h"
  51. #endif
  52.  
  53. #ifndef FWMENU_H
  54. #include "FWMenu.h"
  55. #endif
  56.  
  57. #ifndef FWBARRAY_H
  58. #include "FWBArray.h"
  59. #endif
  60.  
  61. #ifndef FWCFMRES_H
  62. #include "FWCfmRes.h"
  63. #endif
  64.  
  65. // ----- Foundation Layer -----
  66.  
  67. #ifndef FWNOTIFN_H
  68. #include <FWNotifn.h>
  69. #endif
  70.  
  71. #ifndef FWCLAINF_H
  72. #include <FWClaInf.h>
  73. #endif
  74.  
  75. // ----- OpenDoc Includes -----
  76.  
  77. #ifndef _ODTYPES_
  78. #include <ODTypes.h>
  79. #endif
  80.  
  81. #ifndef SOM_Module_OpenDoc_StdProps_defined
  82. #include <StdProps.xh>
  83. #endif
  84.  
  85. #ifndef SOM_ODStorageUnit_xh
  86. #include <StorageU.xh>
  87. #endif
  88.  
  89. #ifndef SOM_ODTranslation_xh
  90. #include <Translt.xh>
  91. #endif
  92.  
  93. #ifndef SOM_ODSession_xh
  94. #include <ODSessn.xh>
  95. #endif
  96.  
  97. #ifndef SOM_ODDragItemIterator_xh
  98. #include <DgItmIt.xh>
  99. #endif
  100.  
  101. // ----- Macintosh Includes -----
  102. #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
  103. #include <Sound.h>
  104. #endif
  105.  
  106.  
  107. //========================================================================================
  108. //    Runtime Information
  109. //========================================================================================
  110.  
  111. #pragma segment ButtonPart
  112.  
  113. //========================================================================================
  114. //    Globals
  115. //========================================================================================
  116.  
  117. const ODValueType CButtonPart::kPartKind = kODFButtonKind;
  118. const ODValueType CButtonPart::kPartUserName = kODFButtonEditorUserString;
  119.  
  120. //========================================================================================
  121. //    class CButtonPart
  122. //========================================================================================
  123.  
  124. //----------------------------------------------------------------------------------------
  125. //    CButtonPart::CButtonPart
  126. //----------------------------------------------------------------------------------------
  127.  
  128. CButtonPart::CButtonPart(ODPart* odPart) :
  129.     FW_CPart(odPart, CButtonPart::kPartKind, CButtonPart::kPartUserName, FW_gInstance, kPartIconID),
  130.     fAction(NULL)
  131. {
  132. }
  133.  
  134. //----------------------------------------------------------------------------------------
  135. //    CButtonPart::~CButtonPart
  136. //----------------------------------------------------------------------------------------
  137.  
  138. CButtonPart::~CButtonPart()
  139. {
  140.     delete fAction;
  141. }
  142.  
  143. //----------------------------------------------------------------------------------------
  144. //    CButtonPart::Initialize
  145. //----------------------------------------------------------------------------------------
  146.  
  147. void CButtonPart::Initialize(Environment* ev)
  148. {
  149.     FW_CPart::Initialize(ev);
  150.     
  151.     // ----- Register our Presentation
  152.     CButtonSelection *selection = 
  153.                             new CButtonSelection(ev, this);
  154.     RegisterPresentation(ev, kODFButtonPresentation, 
  155.                             TRUE, selection);
  156. }
  157.  
  158. //----------------------------------------------------------------------------------------
  159. //    CButtonPart::NewFrame
  160. //----------------------------------------------------------------------------------------
  161.  
  162. FW_CFrame* CButtonPart::NewFrame(Environment* ev,
  163.                              ODFrame* frame,
  164.                              FW_CPresentation* presentation,
  165.                              FW_Boolean storage)
  166. {
  167.     return new CButtonFrame(ev, frame, presentation, this);
  168. }
  169.  
  170. //----------------------------------------------------------------------------------------
  171. // CButtonPart::InternalizeContent
  172. //----------------------------------------------------------------------------------------
  173.  
  174. void CButtonPart::InternalizeContent(Environment *ev, 
  175.                                 ODStorageUnit* storage, 
  176.                                 FW_CCloneInfo* cloneInfo)
  177. {
  178.     FW_ASSERT(fAction == NULL);
  179.     
  180.     if (CScriptAction::IsInStorage(ev, storage))
  181.         fAction = new CScriptAction();
  182.     else if (CSoundAction::IsInStorage(ev, storage))
  183.         fAction = new CSoundAction();
  184.     
  185.     if (fAction != NULL)
  186.         fAction->Internalize(ev, storage);
  187. }
  188.  
  189. //----------------------------------------------------------------------------------------
  190. // CButtonPart::ExternalizeContent
  191. //----------------------------------------------------------------------------------------
  192. void CButtonPart::ExternalizeContent(Environment *ev, 
  193.                                 ODStorageUnit* storage, 
  194.                                 FW_CCloneInfo* cloneInfo)
  195. {
  196.     // ----- Remove script value if exist -----
  197.     if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))
  198.     {
  199.         storage->Focus(ev, kODPropContents, kODPosUndefined, kScriptScrapKind, 0, kODPosUndefined);
  200.         storage->Remove(ev);
  201.     }
  202.     
  203.     // ----- Remove sound value if exist -----
  204.     if (storage->Exists(ev, kODPropContents, kSoundScrapKind, 0))
  205.     {
  206.         storage->Focus(ev, kODPropContents, kODPosUndefined, kSoundScrapKind, 0, kODPosUndefined);
  207.         storage->Remove(ev);
  208.     }
  209.  
  210.     if (fAction != NULL)
  211.         fAction->Externalize(ev, storage);
  212. }
  213.  
  214. //----------------------------------------------------------------------------------------
  215. // CButtonPart::DoAction
  216. //----------------------------------------------------------------------------------------
  217. void CButtonPart::DoAction()
  218. {
  219.     if (fAction != NULL)
  220.         fAction->DoIt();
  221. }
  222.  
  223. //----------------------------------------------------------------------------------------
  224. //    CButtonPart::SetAction
  225. //----------------------------------------------------------------------------------------
  226.  
  227. void CButtonPart::SetAction(CAction *action)
  228. {
  229.     if (action!=NULL && action!=fAction)
  230.     {
  231.         delete fAction;
  232.         fAction = action;
  233.     }
  234. }
  235.  
  236. //========================================================================================
  237. //    class CButtonFrame
  238. //========================================================================================
  239.  
  240. //----------------------------------------------------------------------------------------
  241. //    CButtonFrame::CButtonFrame
  242. //----------------------------------------------------------------------------------------
  243.  
  244. CButtonFrame::CButtonFrame(Environment* ev, 
  245.                     ODFrame* frame, 
  246.                     FW_CPresentation* presentation, 
  247.                     CButtonPart* part) :
  248.     FW_CFrame(ev, frame, presentation, part),
  249.     fButtonPart(part),
  250.     fButton(NULL),
  251.     fButtonId(0),
  252.     fConnection(this),
  253.     fButtonNotificationToken(0)
  254. {
  255.     // I don't want any focus
  256.     FW_CFocusSet focusSet(ev, part->GetSession(ev));
  257.     SetFocusSet(ev, focusSet);
  258.  
  259.     fConnection.Connect();
  260.         
  261.     SetDroppable(ev, TRUE);
  262. }
  263.  
  264. //----------------------------------------------------------------------------------------
  265. //    CButtonFrame::~CButtonFrame
  266. //----------------------------------------------------------------------------------------
  267.  
  268. CButtonFrame::~CButtonFrame()
  269. {
  270.     FW_CInterest interest(fButton, fButtonNotificationToken);
  271.     fConnection.RemoveInterest(interest);
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. //    CButtonFrame::CreateSubViews
  276. //----------------------------------------------------------------------------------------
  277.  
  278. void CButtonFrame::CreateSubViews(Environment* ev)
  279. {
  280.     FW_CString32 label("Button");
  281.     
  282.     FW_CRect frameRect = this->GetBounds(ev);
  283.  
  284.     fButton = new FW_CPushButton(ev, this, fButtonId, frameRect, label);
  285.     fButton->SetNextEventHandler(ev, this);
  286.     
  287.     //     The role of the COptionBehavior is to detect Option-Click. In this case change the focus
  288.     //    set of the frame. Having a behavior allows us to not have to subclass FW_CPushButton
  289.     COptionBehavior* optionBehavior = new COptionBehavior(ev, this, GetPart(ev)->GetSession(ev));
  290.     fButton->AdoptEventHandler(ev, optionBehavior);
  291.     
  292.     fButton->SetDefault(ev, false);
  293.     fButtonNotificationToken = fButton->GetButtonPressedNotificationToken(ev);
  294.     FW_CInterest interest(fButton, fButtonNotificationToken);
  295.     fConnection.AddInterest(interest);
  296. }
  297.  
  298. //----------------------------------------------------------------------------------------
  299. //    CButtonFrame::CanAcceptDrop
  300. //----------------------------------------------------------------------------------------
  301.  
  302. ODDragResult CButtonFrame::CanAcceptDrop(Environment* ev, 
  303.                             ODDragItemIterator* dragInfo)
  304. {
  305.     ODDragResult acceptDrop = 
  306.                 FW_CFrame::CanAcceptDrop(ev, dragInfo);
  307.  
  308.     if (!acceptDrop)
  309.     {
  310.         for (ODStorageUnit *dragSU = dragInfo->First(ev); 
  311.                             dragSU != NULL; 
  312.                             dragSU = dragInfo->Next(ev))
  313.         {
  314.             if (CSoundAction::IsInStorage(ev, dragSU))
  315.                 return TRUE;
  316.             if (CScriptAction::IsInStorage(ev, dragSU))
  317.                 return TRUE;
  318.         }
  319.     }
  320.  
  321.     return acceptDrop;
  322. }
  323.  
  324. //----------------------------------------------------------------------------------------
  325. //    CButtonFrame::Draw
  326. //----------------------------------------------------------------------------------------
  327.  
  328. void CButtonFrame::Draw(Environment* ev, 
  329.                         ODFacet* facet, 
  330.                         ODShape* invalidShape)
  331. {
  332.     FW_CFacetContext fc(ev, facet, invalidShape);
  333.     
  334.     // Just erase and assume gadgets will draw
  335.     FW_CRect invalidRect;
  336.     fc.GetClipRect(invalidRect);
  337.     FW_CRectShape::RenderRect(fc, invalidRect, 
  338.                             FW_kFill, FW_kWhiteEraseInk);
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------
  342. //    CButtonFrame::HandleNotification
  343. //----------------------------------------------------------------------------------------
  344.  
  345. void CButtonFrame::HandleNotification(
  346.                     const FW_CNotification& notification)
  347. {
  348.     const FW_CButtonPressedNotification& buttonPressed =
  349.         (FW_CButtonPressedNotification&) notification;
  350.     
  351.     if (buttonPressed.GetButtonId() == fButtonId)
  352.     {
  353.         fButtonPart->DoAction();
  354.     }
  355. }
  356.  
  357. //----------------------------------------------------------------------------------------
  358. //    CButtonFrame::AdjustSubViews
  359. //----------------------------------------------------------------------------------------
  360.  
  361. void CButtonFrame::AdjustSubViews(Environment* ev)
  362. {
  363.     FW_CRect frameRect = GetBounds(ev);
  364.     fButton->SetSize(ev, frameRect.BotRight());
  365.  
  366.     // Redraw the entire frame
  367.     this->Invalidate(ev);
  368. }
  369.  
  370. //----------------------------------------------------------------------------------------
  371. //    CButtonFrame::FocusStateChanged
  372. //----------------------------------------------------------------------------------------
  373. //    When overridden, call inherited::FocusStateChanged FIRST
  374.  
  375. void CButtonFrame::FocusStateChanged(Environment *ev, ODTypeToken focus, FW_Boolean newState, ODFrame* newOwner)
  376. {
  377.     FW_CFrame::FocusStateChanged(ev, focus, newState, newOwner);
  378.     
  379.     // ----- if I just lost the selection focus then reset
  380.     // ----- the focusset to empty 
  381.     if (!newState && focus == FW_CPart::gSelectionFocusToken)
  382.     {
  383.         FW_CFocusSet focusSet(ev, GetPart(ev)->GetSession(ev));
  384.         SetFocusSet(ev, focusSet);
  385.     }
  386. }
  387.  
  388. //========================================================================================
  389. //    class COptionBehavior
  390. //========================================================================================
  391.  
  392. //----------------------------------------------------------------------------------------
  393. //    COptionBehavior::COptionBehavior
  394. //----------------------------------------------------------------------------------------
  395.  
  396. COptionBehavior::COptionBehavior(Environment* ev, CButtonFrame* frame, ODSession* session) :
  397.     FW_MEventHandler(ev, kNoIdentifier, NULL, TRUE, kNoPriority),
  398.     fFrame(frame),
  399.     fFullFocusSet(ev, session),
  400.     fEmptyFocusSet(ev, session)
  401. {
  402.     fFullFocusSet.Add(ev, FW_CPart::gKeyFocusToken);
  403.     fFullFocusSet.Add(ev, FW_CPart::gMenuFocusToken);
  404.     fFullFocusSet.Add(ev, FW_CPart::gSelectionFocusToken);
  405.     fFullFocusSet.Add(ev, FW_CPart::gClipboardFocusToken);
  406. }
  407.  
  408. //----------------------------------------------------------------------------------------
  409. //    COptionBehavior::~COptionBehavior
  410. //----------------------------------------------------------------------------------------
  411.  
  412. COptionBehavior::~COptionBehavior() 
  413. {
  414. }
  415.  
  416. //----------------------------------------------------------------------------------------
  417. //    COptionBehavior::DoMouseDown
  418. //----------------------------------------------------------------------------------------
  419.  
  420. FW_Boolean COptionBehavior::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent) 
  421. {
  422.     FW_Boolean optionPressed = theMouseEvent.IsCopyModifier(ev);
  423.     
  424.     fFrame->SetFocusSet(ev, optionPressed ? fFullFocusSet : fEmptyFocusSet);
  425.  
  426.     return optionPressed;
  427. }
  428.